<?php

/*
 * Controller/FileController.php
 */

namespace RHX\Controller;
use RHX\Model\Manager\FileManager;
use RHX\Helper\Uploader;

class FileController extends RHXController {

    /**
     * Affichage du formulaire de téléchargement de fichier
     * 
     * @return \Zend\View\Model\ViewModel
     */
    public function uploadAction() {  
        //param        
        $request = $this->getRequest(); 
        $query = $request->getQuery();
        $post = $request->getPost();
        
        $idElt = (int) $this->params()->fromRoute('id', 0);
                
        // flag d' upload
        
        $flag = isset($_GET['flag'] ) ? $_GET['flag'] : null ;        
        if(!$flag)
            throw new \Exception ('Parametre invalide');

        // manager 
        $fileManager = new \RHX\Model\Manager\FileManager($this->getAdapter());        
        
        $fileManager->setFlag($flag);
        
        $repertory = $fileManager->getUploadDirectory();
        
        $this->layout('layout/iframe2');
        
        //param view        
        $paramView = array();
        $paramView ['idElt']    = $idElt;         
        $paramView ['repertory']= $repertory; 
        $paramView ['flag']     = $fileManager->getFlag(); 
        $paramView ['actionSaving'] = $fileManager->getActionSaving();
        $paramView ['titleView'] = $fileManager->getTitleView();
        
        if ($request->isPost()) { 
            $random = rand();
            $name = $repertory.$random.'.tmp';
            
            // Téléchargement du fichier 
            $extensions = $fileManager->getExtensions();            
            $result = Uploader::upload($fileManager->getFlag(), $name, FALSE, $extensions);
            $result['destination'] = "$random.tmp";
            
            if ($result['done']) {
               
                $idElt = (int) $post->id_elt;
                if ($idElt != 0) {
                    if(isset($post->conserve_name) && (int)$post->conserve_name){
                        $fileName = $result['name'];
                    }
                    else{
                        if($flag == FileManager::SITE_DOC_FILE){
                            $arrayFormat = array($idElt, md5(time()));
                        }
                        else{
                            $arrayFormat = array($idElt) ;                            
                        }
                        $fileName = $fileManager->formatName( $arrayFormat ) . '.' . $result['extension'];
                    }
                    
                    $newName  = $repertory. $fileName;
                    $newName2 = str_replace('public/', '', $repertory).$fileName;
                    
                    // renommer fichier
                    if (@rename($name, $newName) || @rename($name,  $newName2)) {
                        $result['destination'] = $fileName;
                    }
                }
                $paramView['fichier']     = $result['name'];
                $paramView['destination'] = $result['destination'];                
                $paramView['extension']   = $result['extension'];
                $paramView['error']       = $result['error'];
  
                return $paramView ;
            }
            elseif( $result['error'] ){ 
                $paramView['fichier']     = null;
                $paramView['error']       = $result['error'];
                
                return $paramView ;
            }
        }
               
        $paramView ['fichier']  = null; 
        $paramView ['error']  = null; 
        
        return $paramView;        
    }

}

?>
